using Catalog.Application.CatalogItems.Interfaces;
using Catalog.Application.CatalogItems.Queries;
using Catalog.Application.Contracts.DTOs;
using MediatR;

namespace Catalog.Application.CatalogItems.QueryHandlers;

public class GetCatalogItemByIdQueryHandler : IRequestHandler<GetCatalogItemByIdQuery, CatalogItemProfileDto?>
{
    private readonly ICatalogItemReadService _catalogItemReadService;

    public GetCatalogItemByIdQueryHandler(ICatalogItemReadService catalogItemReadService)
    {
        _catalogItemReadService = catalogItemReadService;
    }

    public async Task<CatalogItemProfileDto?> Handle(GetCatalogItemByIdQuery request, CancellationToken cancellationToken)
    {
        var catalogItem = await _catalogItemReadService.GetByIdAsync(request.id, cancellationToken);
        if (catalogItem == null)
        {
            return null;
        }

        return new CatalogItemProfileDto
        {
            Id = catalogItem.Id,
            Name = catalogItem.Name,
            Description = catalogItem.Description,
            Price = catalogItem.Price,
            BrandId = catalogItem.BrandId,
            CategoryId = catalogItem.CategoryId
        };
    }
}